<template>
  <view>
    <view>
      <slot name="top"></slot>
    </view>
    <view class="ss-order-card-warp ss-flex ss-col-stretch ss-row-between bg-white"
          :style="[{ borderRadius: radius + 'rpx', marginBottom: marginBottom + 'rpx' }]">
      <view class="img-box ss-m-r-24">
        <image class="order-img" :src="sheep.$url.cdn(img)" mode="aspectFill"></image>
      </view>
      <view class="box-right ss-flex-col ss-row-between"
            :style="[{ width: titleWidth ? titleWidth + 'rpx' : '' }]">
        <view class="title-text ss-line-2" v-if="title">{{ title }}</view>
        <view v-if="skuString" class="spec-text ss-m-t-8 ss-m-b-12">{{ skuString }}</view>
        <view class="groupon-box">
          <slot name="groupon"></slot>
        </view>
        <view class="ss-flex">
          <view class="ss-flex ss-col-center">
            <view class="price-text ss-flex ss-col-center" :style="[{ color: priceColor }]"
                  v-if="price && Number(price) > 0">
              ￥{{ price }}
            </view>
            <view v-if="num" class="total-text ss-flex ss-col-center">x {{ num }}</view>
            <slot name="priceSuffix"></slot>
          </view>
        </view>
        <view class="tool-box">
          <!-- 选中某订单 -->
          <slot name="tool">
            <checkbox-group @change="handleCheckboxChange">
              <label>
                {{ radioData.text }}
                <checkbox :value="props.itemValue" :checked="false" :disabled="radioData.disabled"/>
              </label>
            </checkbox-group>
          </slot>
        </view>
        <view>
          <slot name="rightBottom"></slot>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import sheep from '@/sheep';
import {
  computed,
  defineEmits,
  ref,
  reactive,
  watch
} from 'vue';

/**
 * 订单卡片
 *
 * @property {String} img                      - 图片
 * @property {String} title                    - 标题
 * @property {Number} titleWidth = 0								- 标题宽度，默认0，单位rpx
 * @property {String} skuText                    - 规格
 * @property {String | Number} price                - 价格
 * @property {String} priceColor                  - 价格颜色
 * @property {Number | String} num                  - 数量
 *
 */
const props = defineProps({
  img: {
    type: String,
    default: 'https://img1.baidu.com/it/u=1601695551,235775011&fm=26&fmt=auto',
  },
  title: {
    type: String,
    default: '',
  },
  titleWidth: {
    type: Number,
    default: 0,
  },
  skuText: {
    type: [String, Array],
    default: '',
  },
  price: {
    type: [String, Number],
    default: '',
  },
  priceColor: {
    type: [String],
    default: '',
  },
  num: {
    type: [String, Number],
    default: 0,
  },
  score: {
    type: [String, Number],
    default: '',
  },
  radius: {
    type: [String],
    default: '',
  },
  marginBottom: {
    type: [String],
    default: '',
  },
  initialItems: {
    type: [Array],
    default: []
  },
  itemValue: {
    type: Object,
    default: 0,
  },
  radioData: {
    type: Object,
    default: {},
  }
});
const skuString = computed(() => {
  if (!props.skuText) {
    return '';
  }
  if (typeof props.skuText === 'object') {
    return props.skuText.join(',');
  }
  return props.skuText;
});

// 订单数据添加
const emit = defineEmits(['update-items'])
const childArray = ref([...props.initialItems]);

// 当props变化时更新本地数组
watch(() => props.initialItems, (newVal) => {
  childArray.value = [...newVal];
});

const handleCheckboxChange = (e) => {
  // 根据选中状态更新数组
  if (e.detail.value[0]) {
    // 如果被选中，且不在数组中，则添加到数组
    if (!childArray.value.includes(props.itemValue)) {
      childArray.value.push(props.itemValue)
      console.log('添加:', childArray.value);
    }
  } else {
    // 如果取消选中，从数组中移除该项
    childArray.value = childArray.value.filter(item => item !== props.itemValue);
    console.log('移除:', childArray.value);
  }

  emit('update-items', childArray.value);
};

</script>

<style lang="scss" scoped>
.score-img {
  width: 36rpx;
  height: 36rpx;
  margin: 0 4rpx;
}

.ss-order-card-warp {
  padding: 20rpx;

  .img-box {
    width: 164rpx;
    height: 164rpx;
    border-radius: 10rpx;
    overflow: hidden;

    .order-img {
      width: 164rpx;
      height: 164rpx;
    }
  }

  .box-right {
    flex: 1;
    // width: 500rpx;
    // height: 164rpx;
    position: relative;

    .tool-box {
      position: absolute;
      right: 0rpx;
      bottom: -10rpx;
    }
  }

  .title-text {
    font-size: 28rpx;
    font-weight: 500;
    line-height: 40rpx;
  }

  .spec-text {
    font-size: 24rpx;
    font-weight: 400;
    color: $dark-9;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
  }

  .price-text {
    font-size: 24rpx;
    font-weight: 500;
    font-family: OPPOSANS;
  }

  .total-text {
    font-size: 24rpx;
    font-weight: 400;
    line-height: 24rpx;
    color: $dark-9;
    margin-left: 8rpx;
  }
}
</style>
